local ConstructManager = {}
ConstructManager.mt = { __index = ConstructManager }
setmetatable(ConstructManager, ConstructManager.mt)

function ConstructManager:initialize()

	--local cm = {}
	--setmetatable(cm, ConstructManager.mt)

	--cm.
	self.constructs = {}
	--cm.
	self.ships = {}
	--cm.
	self.docks = {}
	--cm.
	self.tridocks = {}
	
	--return cm
end


function ConstructManager.new()

	local o = ConstructManager.initialize()

	return o

end

function ConstructManager:update(dt)
	local con_man_upd = function(self, dt)
	
		self:update_ships(dt)
		coroutine.yield()
	end
	
	
	local a = MOAICoroutine.new()
	a:run(con_man_upd, self, dt)
	

end

function ConstructManager:update_ships(dt)

	
	for i = 1, #self.ships do
		
		
		self.ships[i]:update(dt)
	
	end

end

function ConstructManager:add( construct )
	local iter = #self.constructs+1
	self.constructs[iter] = construct
	return iter

end

function ConstructManager:add_ship ( ship )
	local iter = #self.ships+1
	
	self.ships[iter] = ship
	
	return iter


end

function ConstructManager:add_dock ( dock )

	local iter = #self.docks+1
	
	self.docks[iter] = dock
	
	return iter

end

function ConstructManager:spawn_ship( layout, x, y, offsets, wire_map )
	
	--local ships = self:get_ships()
	--local constructs = self:get_constructs()
	
	local iter = #self.ships+1
	local iter2 = #self.constructs+1
	
	self.constructs[iter2] = Ship.new(layout, x, y, offsets, wire_map )
	self.constructs[iter2].id = iter2
	self.constructs[iter2].ship_id = iter
	self.constructs[iter2]:connect_electric()
	self.constructs[iter2]:man_stations()
	self.constructs[iter2]:build_dmgable()
	self.ships[iter] = self.constructs[iter2]
	
	
	return self.ships[iter]
	

end



function ConstructManager:spawn_dock( x, y, w, h )

	--local docks = self:get_docks()
	--local constructs = self:get_constructs()
	
	local iter = #self.docks+1
	local iter2 = #self.constructs+1
	
	if not h or h < 3 then h = 3 end
	if not w or w < 3 then w = 3 end
	
	
	self.constructs[iter2] = DockPlatform.new( x, y, x+w, y+h)
	self.constructs[iter2].id = iter2
	self.constructs[iter2].dock_id = iter
	self.docks[iter] = self.constructs[iter2]
	
	return self.docks[iter]

end

function ConstructManager:spawn_tridock( x, y, w, h )

	--local tridocks = self:get_tridocks()
	--local constructs = self:get_constructs()

	local iter = #self.tridocks+1
	local iter2 = #self.constructs+1
	
	if not h or h < 3 then h = 3 end
	if not w or w < 3 then w = 3 end
	
	
	
	self.constructs[iter2] = Tridock.new( x, y, w, h)

	self.constructs[iter2].id = iter2
	
	self.constructs[iter2].tridock_id = iter
	
	self.tridocks[iter] = self.constructs[iter2]
	
	
	return self.tridocks[iter]


end

function ConstructManager:get_ships()

	return self.ships

end

function ConstructManager:get_ship(id)

	return self.ships[id]

end

function ConstructManager:get_constructs()

	return self.constructs
	
end

function ConstructManager:get_docks()


	return self.docks
	
end

function ConstructManager:get_tridocks()


	return self.tridocks
	
end

function ConstructManager:remove( construct_id )

	table.remove(self.constructs, construct_id)

end

function ConstructManager:remove_ship( ship_id )

	table.remove(self.ships, ship_id)

end

function ConstructManager:remove_dock( dock_id )

	table.remove(self.docks, dock_id)

end

function ConstructManager:remove_tridock( tridock_id )

	table.remove(self.tridocks, tridock_id)

end


return ConstructManager